package edu.tjhsst.androidIntranet.news;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import edu.tjhsst.androidIntranet.R;
import edu.tjhsst.androidIntranet.db.IntranetDbAdapter;

public class NewsAdapter implements ListAdapter {

	//private LinkedList<NewsEntry> list;
	private Context c;
	private DateFormat dateFormatter;
	IntranetDbAdapter.NewsDbAdapter nDbA;
	Cursor newsCursor;

	public NewsAdapter(Context c) {
		this.c=c;
		nDbA = IntranetDbAdapter.getNewsDbAdapter(c);
		reInitCursor();
		this.dateFormatter = SimpleDateFormat.getDateInstance();
	}

	private void reInitCursor() {
		newsCursor = nDbA.getNewsCursor();
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public boolean isEnabled(int position) {
		return true;
	}

	@Override
	public int getCount() {
		return newsCursor.getCount();
	}

	@Override
	public NewsEntry getItem(int i) {
		newsCursor.moveToPosition(i);
		return new NewsEntry(newsCursor.getInt(0), newsCursor.getString(1), newsCursor.getString(2), newsCursor.getString(3), new Date(newsCursor.getLong(4)), newsCursor.getInt(5)==1);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.d("NewsAdapter", "View Requested");
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.news_content_row, null);
		}
		NewsEntry n =this.getItem(position);
		if(n != null) {
			TextView title = (TextView) v.findViewById(R.id.title);
			TextView content = (TextView) v.findViewById(R.id.content);
			TextView date = (TextView) v.findViewById(R.id.date);
			if(title != null) {
				title.setText(n.getTitle());
			}
			if(content != null) {
				content.setText(n.getContent());
			}
			if(date != null) {
				date.setText(dateFormatter.format(n.getDatePosted()));
			}
		}
		return v;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		if(newsCursor.getCount()>0) {
			return false;
		}
		return true;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub

	}

	public void insert(int identifier, String title, String content, String poster, Date datePosted, boolean read) {
		nDbA.insertNews(identifier, title, content, poster, datePosted.getTime(), read);
	}

	public void updateList() {
		newsCursor.requery();
	}

}
